<?php
/*
 * 购物车
 * */
namespace app\home\controller\user;

use app\common\model\ShopBundlingGoodsModel;
use app\common\model\ShopBundlingModel;
use app\common\model\ShopCartModel;
use app\common\model\ShopGoodsModel;
use think\Request;

class CartController extends BaseController
{

    private $cartModel;
    private $goodsModel;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->cartModel  = new ShopCartModel();
        $this->goodsModel = new ShopGoodsModel();
    }

    /**
     * 购物车列表
     */
    public function index()
    {
        $cartList = $this->cartModel->cartList(['buyer_uid' => $this->getMemberUid()]);
        $cartList = $this->cartModel->refreshCart($cartList);
        //购物车商品以店铺ID分组显示,并计算商品小计,店铺小计与总价由JS计算得出
        $siteCartList = [];
        foreach ($cartList as $cart) {
            $cart['total_cart_price'] += $cart['cart_price']*$cart['cart_num'];
            $siteCartList[$cart['site_id']][] = $cart;
        }
        if ($this->request->isAjax()) {
            return json($siteCartList);
        }
        $this->assign('siteCartList', $siteCartList);
        return $this->fetch();
    }

    /**
     * 添加购物车
     */
    public function addCart()
    {
        $skuId   = $this->request->param('sku_id/d');
        $number  = $this->request->param('number/d');
        $blId    = $this->request->param('bl_id/d', 0); //优惠套装Id
        $skuInfo = [];
        if ($skuId > 0) { //加入普通商品
            $skuInfo = $this->goodsModel->getSkuInfo($skuId);
            $this->_check_sku($skuInfo, $number);
        }
        if ($blId > 0) { //加入套装商品
            $bundlingModel      = new ShopBundlingModel();
            $bundlingGoodsModel = new ShopBundlingGoodsModel();
            $blInfo             = $bundlingModel->where('bl_id', $blId)->find();
            if (empty($blInfo) || $blInfo['status'] == '0') {
                $this->error('该优惠套装已不存在，建议您单独购买！');
            }
            //检查每个商品是否符合条件
            $blGoodsList        = $bundlingGoodsModel->where('bl_id', $blId)->column('*', 'sku_id');
            $online_blGoodsList = goods_all_list(0, 0, 50, ['sku_id'=>['in',array_keys($blGoodsList)]]);
            foreach ($online_blGoodsList as $sku) {
                $this->_check_sku($sku, 1);
            }
            //优惠套装作为一条记录插入购物车，商品信息取套装内的第一个商品,包装量和最小起订量设置为1
            $skuInfo                = [];
            $skuInfo['site_id']     = $blInfo['site_id'];
            $skuInfo['site_name']   = $blInfo['site_name'];
            $skuInfo['goods_id']    = $online_blGoodsList[0]['goods_id'];
            $skuInfo['sku_id']      = $online_blGoodsList[0]['sku_id'];
            $skuInfo['goods_name']  = $blInfo['bl_name'];
            $skuInfo['minimum']     = 1;
            $skuInfo['step']        = 1;
            $skuInfo['goods_unit']  = $online_blGoodsList[0]['goods_unit'];
            $skuInfo['goods_thumb'] = $online_blGoodsList[0]['goods_thumb'];
            $skuInfo['goods_price'] = $blInfo['total_simple_price'];
            $skuInfo['cart_price']  = $blInfo['bl_discount_price'];
            $skuInfo['cart_num']    = $number;
            $skuInfo['bl_id']       = $blId;
        }
        $result = $this->cartModel->addCart($this->getMemberUid(), $skuInfo, $number);
        if ($result === false) {
            $this->error($this->cartModel->getError());
        }
        $this->success('加入购物车成功！');
    }

    /**
     * 删除购物车
     */
    public function delCart()
    {
        $cartId = $this->request->param('cart_id/a');
        $this->cartModel->delCart(['card_id' => $cartId]);
        $this->success('删除成功！');
    }

    /**
     * 清空购物车
     */
    public function clearCart()
    {
        $this->cartModel->delCart(['buyer_uid' => $this->getMemberUid()]);
        $this->success('操作成功！');
    }


    /**
     * 检查商品是否符合加入购物车条件
     * @param array $skuInfo
     * @param number $number
     */
    private function _check_sku($skuInfo, $number)
    {
        if ((int)$number < 1) {
            $this->error('购买数量不明确！');
        }
        if (empty($skuInfo)) {
            $this->error('商品信息不存在！');
        }
        if (intval($skuInfo['goods_stock']) < 1) {
            $this->error('商品已售罄！');
        }
        if (intval($skuInfo['goods_stock']) < $number) {
            $this->error('商品库存不足！');
        }
        //检测最小起订量
        if ($skuInfo['minimum'] > $number) {
            $this->error('该商品最小起订量为' . $skuInfo['minimum']);
        }
        //检测活动商品
        if ($skuInfo['is_presell'] == 1 || in_array($skuInfo['promotion_type'], [2, 3])) {
            $this->error('该商品不允许加入购物车，请直接购买！');
        }
    }

}
